package com.example.wechat;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.BaseRequestOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.target.ViewTarget;
import com.bumptech.glide.request.transition.Transition;

public class ImageLoader {
    private static final int RETRY_COUNT = 3;
    private static final int RETRY_DELAY = 1000; // 1秒

    public static void loadCircleImage(ContactDetailActivity contactDetailActivity, String avatarUrl, ImageView avatarImageView) {

    }

    public interface LoadCallback {
        void onSuccess();
        void onError(String error);
        void onProgress(int progress);
    }

    public static void loadImage(Context context, String url, ImageView imageView) {
        loadImage(context, url, imageView, null);
    }

    private static void loadImage(Context context, String url, ImageView imageView, Integer o) {
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int placeholder) {
        loadImage(context, url, imageView, placeholder, null);
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int placeholder, LoadCallback callback) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholder)
                .error(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop();

        ViewTarget<ImageView, Drawable> into = Glide.with(context)
                .load(url)
                .apply(options)
                .transition(DrawableTransitionOptions.withCrossFade())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable java.lang.Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        if (callback != null) {
                            callback.onError(e != null ? e.getMessage() : "加载失败");
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull java.lang.Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                        return false;
                    }
                })
                .into(imageView);
    }

    public static void preloadImage(Context context, String url) {
        Glide.with(context)
                .load(url)
                .preload();
    }

    public static void loadImageWithRetry(Context context, String url, ImageView imageView, @DrawableRes int placeholder) {
        loadImageWithRetry(context, url, imageView, placeholder, RETRY_COUNT);
    }

    private static void loadImageWithRetry(Context context, String url, ImageView imageView, @DrawableRes int placeholder, int retryCount) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholder)
                .error(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop();

        Glide.with(context)
                .load(url)
                .apply(options)
                .transition(DrawableTransitionOptions.withCrossFade())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable java.lang.Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        if (retryCount > 0) {
                            imageView.postDelayed(() -> {
                                loadImageWithRetry(context, url, imageView, placeholder, retryCount - 1);
                            }, RETRY_DELAY);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull java.lang.Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);
    }

    public static void loadImageWithProgress(Context context, String url, ImageView imageView, @DrawableRes int placeholder, LoadCallback callback) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholder)
                .error(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop();

        Glide.with(context)
                .load(url)
                .apply(options)
                .transition(DrawableTransitionOptions.withCrossFade())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable java.lang.Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull java.lang.Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new ProgressTarget<Drawable>(imageView) {
                    @Override
                    protected void setResource(@Nullable Drawable resource) {

                    }

                    @Override
                    public void onProgress(int progress) {
                        if (callback != null) {
                            callback.onProgress(progress);
                        }
                    }

                    @Override
                    protected void onProgressUpdate(int progress) {

                    }
                });
    }

    public static void loadAvatar(Context context, String url, ImageView imageView) {
        BaseRequestOptions<?> DEFAULT_OPTIONS = null;
        Glide.with(context)
                .load(url)
                .apply(DEFAULT_OPTIONS)
                .circleCrop()
                .into(imageView);
    }

    public static void loadImageWithSize(Context context, String url, ImageView imageView, int width, int height) {
        BaseRequestOptions<?> DEFAULT_OPTIONS = null;
        Glide.with(context)
                .load(url)
                .apply(DEFAULT_OPTIONS)
                .override(width, height)
                .into(imageView);
    }

    public static void clearMemory(Context context) {
        Glide.get(context).clearMemory();
    }

    public static void clearDiskCache(Context context) {
        Glide.get(context).clearDiskCache();
    }
} 